package com.cscp.javastudy.juc.callable;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableDemo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*
         * 线程的创建方式
         * 1- new Thread(new Thread()).start(); 无返回
         * 2- new Thread(futureTask, "A").start();
         */
        MyThread myThread = new MyThread();
        FutureTask futureTask = new FutureTask(myThread);
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();//这两个线程其实只执行了一次call()，因为结果会缓存，提高效率
        String obj = (String)futureTask.get();//get()可能产生阻塞,可以把它放到最后一行
        System.out.println("result is : " + obj);

    }

}


class MyThread implements Callable<String> {

    @Override
    public String call() {
        System.out.println("====call()");
        return "whoisjama";
    }
}
